﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    /// <summary>
    /// 产品相关帮助文件
    /// </summary>
    public class ProductHelper
    {
        /// <summary>
        /// 根据商品的ID和商品的图片的名称返回图片存放的实际路径
        /// </summary>
        /// <param name="ProductID"></param>
        /// <param name="IMGName"></param>
        /// <returns></returns>
        public static String GetIMGPath(Int32 ProductID, String IMGName)
        {
            string sourceUrl = System.Configuration.ConfigurationManager.AppSettings["sourceUrl"];
            if (string.IsNullOrEmpty(IMGName))
            {
                return sourceUrl + "/product/default.png";
            }
            else
            {
                string md5 = Common.SecurityHelper.Safe.MD5(ProductID.ToString());
                return sourceUrl + "/Product/" + md5.Substring(0, 1) + "/" + md5.Substring(1, 1) + "/" + md5.Substring(2, 1) + "/" + md5.Substring(3, 1) + "/" + IMGName;
            }
        }

        /// <summary>
        /// 根据商品的ID和商品的图片的名称返回图片存放的实际路径
        /// </summary>
        /// <param name="ProductID"></param>
        /// <param name="IMGName"></param>
        /// <returns></returns>
        public static String GetBrandIMGPath(Int32 ProductID, String IMGName)
        {
            string sourceUrl = System.Configuration.ConfigurationManager.AppSettings["sourceUrl"];
            if (string.IsNullOrEmpty(IMGName))
            {
                return sourceUrl + "/BrandProduct/default.png";
            }
            else
            {
                string md5 = Common.SecurityHelper.Safe.MD5(ProductID.ToString());
                return sourceUrl + "/BrandProduct/" + md5.Substring(0, 1) + "/" + md5.Substring(1, 1) + "/" + md5.Substring(2, 1) + "/" + md5.Substring(3, 1) + "/" + IMGName;
            }
        }

        public static String GetUploadProductImgPath(string Assembly, Int32 ProductID)
        {
            string md5 = Common.SecurityHelper.Safe.MD5(ProductID.ToString());

            if (Assembly == "DAL")
            {
                return "/UploadFiles/Product/" + md5.Substring(0, 1) + "/" + md5.Substring(1, 1) + "/" + md5.Substring(2, 1) + "/" + md5.Substring(3, 1) + "/";
            }
            else 
            {
                return "/UploadFiles/BrandProduct/" + md5.Substring(0, 1) + "/" + md5.Substring(1, 1) + "/" + md5.Substring(2, 1) + "/" + md5.Substring(3, 1) + "/";
            }
        }

        public static String GetProductImgUrl(string Assembly, Int32 ProductID)
        {
            string sourceUrl = System.Configuration.ConfigurationManager.AppSettings["sourceUrl"];
            string md5 = Common.SecurityHelper.Safe.MD5(ProductID.ToString());

            if (Assembly == "DAL")
            {
                return sourceUrl.TrimEnd('/') +"/Product/" + md5.Substring(0, 1) + "/" + md5.Substring(1, 1) + "/" + md5.Substring(2, 1) + "/" + md5.Substring(3, 1) + "/";
            }
            else 
            {
                return sourceUrl.TrimEnd('/') + "/BrandProduct/" + md5.Substring(0, 1) + "/" + md5.Substring(1, 1) + "/" + md5.Substring(2, 1) + "/" + md5.Substring(3, 1) + "/";
            }
        }
    }
}
